package com.helloworld.chat.listener;

import com.helloworld.chat.cache.CacheManager;
import com.helloworld.chat.cache.UnReadMessageCache;
import com.helloworld.chat.cache.UserCache;
import com.helloworld.chat.service.UserService;
import com.helloworld.chat.websocket.WebSocket;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.ServletContextAware;

import javax.servlet.ServletContext;

/**
 * Created by xdzy on 2017/7/1.
 */
@Component
public class ServerStartListener implements InitializingBean,ServletContextAware{
    private static final Logger logger=Logger.getLogger(ServerStartListener.class);
    private UserService userService;
    private ServletContext servletContext;
    @Autowired
    public void setUserService(UserService userService) {
        this.userService = userService;
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        UserCache userCache= userService.getUserCache();
        UnReadMessageCache unReadMessageCache=new UnReadMessageCache();
        CacheManager.init(userCache,unReadMessageCache);
        logger.info("init success,user count is "+userCache.getUserCount());
    }

    @Override
    public void setServletContext(ServletContext servletContext) {
        this.servletContext=servletContext;
    }
}
